<?php

class Admin_ProductCategoryController extends Zend_Controller_Action
{
    private $_model;
    public function init()
    {
        /* Initialize action controller here */
        $this->_model = new Application_Model_DbTable_ProductCategory();
    }

    public function indexAction()
    {
        // action body 
        $do = $this->_request->getParam("do");
        if(!empty($do)){
            $id = intval($this->_request->getParam("id"));
            if($do=="local"){
                $value = intval($this->_request->getParam("value"));
                $result = $this->_model->update(array("local"=>$value),"id=$id");
                $this->_helper->json->sendJson($result);
            }
        } 
        
        $choice = $this->_request->getParam("choice");
        if(!empty($choice)){
            $id = intval($this->_request->getParam("id"));
            $row = $this->_model->find($id)->current();
            $n_status = ($row->status)?0:1;
            if($choice=="once"){
                $this->_model->update(array("status"=>0),null);
                $result = $this->_model->update(array("status"=>$n_status),"id=$id");
                $this->_helper->json->sendJson($result);
            }else if($choice=="multi"){
                $result = $this->_model->update(array("status"=>$n_status),"id=$id");
                $this->_helper->json->sendJson($result);
            }
        }     
        
    }

    public function addAction()
    {
       
        // action body
        $form = new Application_Form_ProductCategory();
        if($this->_request->isPost())
        {
            
            if($form->isValid($_POST) && !$row)
            {
                $row = $this->_model->createRow(
                    $form->getValues()
                );
                $row->alias = Cms_Filter_String::toAlias($row->name);
                $row->save();
                $this->_redirect($this->view->url(array(
                    "module"=>"admin",
                    "controller"=>"product-category",
                    "action"=>"index"
                ),null, true));  
            }
            else
            {
                
                $errors = $form->getMessages();
                foreach($errors as $key=>$value)
                {
                    $form->$key->setAttrib("class","error");
                }
            }
        }
        $this->view->form = $form;        
    }

    public function listAction()
    {        
        // action body
         $sortby = $this->_request->getParam("sortby",null);
         $sorttype = $this->_request->getParam("sorttype", "asc");
         $filter_field = $this->_request->getParam("filter_field", null);
         $filter_value = $this->_request->getParam("filter_value");
         $this->view->sort = array("by"=>$sortby, "type"=>$sorttype);
         
         $filter = null;
         if(!empty($filter_field))
         {
            $filter[$filter_field] = $filter_value;
         }
         $adapter = $this->_model->fetchPaginatorAdapter($filter,$sortby,$sorttype);
         $paginator = new Zend_Paginator($adapter);
         
         //Show 10 dòng trên 1 trang
         $paginator->setItemCountPerPage(10);
         $page = $this->_request->getParam("page",1);
         $paginator->setCurrentPageNumber($page);
         $this->view->datas = $paginator;
        $this->_helper->layout()->disableLayout();
        $this->_response->clearBody();
    }

    public function editAction()
    {
        $this->view->headScript()->appendFile(
            $this->view->baseUrl("templates/admin/js/jquery.validate.min.js")
        );
        
        $id = intval($this->_request->getParam("id",null));
        
        if(!empty($id))
        {
            // action body
            $form = new Application_Form_ProductCategory();
            $form->removeElement("id");
            $row = $this->_model->find($id)->current();
            $form->populate($row->toArray());
            
            if($this->_request->isPost())
            {
                if($form->isValid($_POST)&& !$crow)
                {
                    $row->name = $form->getValue("name");
                    $row->description = $form->getValue("description");
                    $row->alias = Cms_Filter_String::toAlias($row->name);
                    $row->save();
                    $this->_redirect($this->view->url(array(
                        "module"=>"admin",
                        "controller"=>"product-category",
                        "action"=>"index"
                    ),null, true));  
                }
                else
                {                    
                    $errors = $form->getMessages();
                    foreach($errors as $key=>$value)
                    {
                        $form->$key->setAttrib("class","error");
                    }
                }
            }
            $this->view->form = $form; 
        }
        
    }

    public function delAction()
    {
        $id = intval($this->_request->getParam("id",null));
        $result = null;
        $row = $this->_model->find($id)->current();
        $result = $row->delete();
        $this->_helper->json->sendJson($id);
    }
}












